﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Mac;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using CPFUI.IHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPFUI.IControl.KeyBord
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class KeyBorderItem : Control
    {
        public KeyBorderItem(Keys keycode,string normalText, string shiftText)
        {
            KeyCode = keycode;
            NormalText = normalText;
            ShiftText = shiftText;
            FontSize = 20;
            UnNumLockKeyCode = 0;
        }
        TextBlock txtValue1;
        TextBlock txtValue2;
        ViewFill keyback = new LinearGradientFill()
        {
            StartPoint = new PointField(0, 0),
            EndPoint = new PointField(0, "100%"),
            GradientStops =
                    {
                        new GradientStop(Color.FromArgb(255,61,61,61), 0),
                        new GradientStop(Color.FromArgb(255,50,50,50), 0.2f),
                        new GradientStop(Color.FromArgb(255,40,40,40), 0.3f),
                        new GradientStop(Color.FromArgb(255,20,20,20), 0.7f),
                        new GradientStop(Color.FromArgb(255,8,8,8), 1f)
                    }
        }; 
        public int UnNumLockKeyCode { get; set; }
        public Visibility IsPressed
        {
            get { return GetValue<Visibility>(); }
            set { SetValue(value); 

            }
        }
        public Visibility IsMouseHover
        {
            get { return GetValue<Visibility>(); }
            set
            {
                SetValue(value);

            }
        }
        public bool IsChar
        {
            get { return GetValue<bool>(); }
            set { SetValue(value);
                if (IsChar)
                {
                    FontSize = 20;
                }
                else
                {
                    FontSize = 15;
                }
            }
        }
        public Keys KeyCode
        {
            get { return GetValue<Keys>(); }
            set { SetValue(value); }
        }
        public string NormalText
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public string ShiftText
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 是否是字母键.
        /// </summary>
        public bool IsLetterKey
        {
            get
            {
                return KeyCode >= Keys.A && KeyCode <= Keys.Z;
            }
        }
        /// <summary>
        /// 是否是数字键
        /// </summary>
        public bool IsNumberPadKey
        {
            get
            {
                return KeyCode >= Keys.NumPad0 && KeyCode <= Keys.Divide;
            }
        }
        /// <summary>
        /// 是否输入法切换
        /// </summary>
        public bool IsIMEKey
        {
            get
            {
                return KeyCode == Keys.IMEModeChange;
            }
        }
        /// <summary>
        /// 是否是Shift,Ctl,Win,Alt键
        /// </summary>
        public bool IsModifierKey
        {
            get
            {
                return KeyCode == Keys.LControlKey
                     || KeyCode == Keys.RControlKey
                     ||KeyCode == Keys.Alt
                     ||KeyCode == Keys.ControlKey
                     || KeyCode == Keys.ShiftKey
                     || KeyCode == Keys.LShiftKey
                     || KeyCode == Keys.RShiftKey
                     || KeyCode == Keys.LWin
                     || KeyCode == Keys.RWin;
            }
        }


        /// <summary>
        /// 是否是锁定键
        /// </summary>
        public bool IsLockKey
        {
            get
            {
                return KeyCode == Keys.Capital
                    || KeyCode == Keys.Scroll
                    || KeyCode == Keys.NumLock;
            }
        }
        //模板定义
        protected override void InitializeComponent()
        {
            IsMouseHover = Visibility.Collapsed;
            IsPressed = Visibility.Visible;
            Cursor = Cursors.Hand;
            IsAntiAlias = true;
            MinHeight = 65;
            MinWidth = 50;
            this.Background = keyback;
            BorderStroke = new Stroke(1);
            BorderFill = Color.FromRgb(6, 12, 16);
            CornerRadius = "4";
            txtValue1 = new TextBlock
            {
                IsAntiAlias = true,
                MarginLeft = 5,
                MarginRight = 2,
                MarginTop = 10,
                MarginBottom = 2,
                TextAlignment = TextAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Text = NormalText,
                Foreground = Color.FromRgb(232, 232, 232),
                FontSize = 15,
                Bindings =
                {
                    {
                        nameof(TextBlock.FontSize),nameof(FontSize),this
                    }
                }
            };
            txtValue2 = new TextBlock
            {
                IsAntiAlias = true,
                MarginLeft = 5,
                MarginTop = 2,
                Visibility = string.IsNullOrEmpty(ShiftText) ? Visibility.Hidden : Visibility.Visible,
                TextAlignment = TextAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Text = ShiftText,
                Foreground = Color.FromRgb(110, 110, 110),
                FontSize = 15,
            };
            Children.Add(
                new Panel
                {
                    IsAntiAlias = true,
                    MarginLeft = 0.5,
                    MarginRight = 0.5,
                    MarginTop = 0.5,
                    MarginBottom = 0.5,
                    CornerRadius = "4",
                    BorderFill = Color.FromRgb(112, 112, 113),
                    BorderStroke = "1,Solid",
                }
                );
            Children.Add(txtValue1);
            Children.Add(txtValue2);
            Children.Add(new Panel {

                BorderFill = null,
                CornerRadius = "4",
                Visibility = IsMouseHover,
                MarginLeft = 0.5,
                MarginRight = 0.5,
                MarginTop = 0.5,
                MarginBottom = 0.5,
                Background =Color.FromArgb(30,255,255,255),
                Bindings =
                {
                    {
                        nameof(Panel.Visibility),nameof(IsMouseHover),this

                    }
                }
            });
            //Children.Add(new Ellipse
            //{
            //    IsAntiAlias= true,
            //    Width=10,
            //    Height=10,
            //    Fill=Color.Green,
            //    StrokeFill=Color.GreenYellow,
            //    MarginTop=5,
            //    Visibility= Visibility.Hidden,
            //    Bindings =
            //    {
            //        {
            //            nameof(Ellipse.Visibility),nameof(IsPressed),this

            //        }
            //    }
            //});
            Children.Add(new Border
            {
                IsAntiAlias = true,
                Width = 30,
                CornerRadius= "4",
                Height = 10,

                BorderFill = Color.FromRgb(50, 50, 50),
               
                Background= Color.Green,
                MarginTop = 5,
                Visibility = IsPressed,
                Bindings =
                {
                    {
                        nameof(Border.Visibility),nameof(IsPressed),this

                    }
                },
            });
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            IsPressed = Visibility.Hidden;
        }
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            if (!IsModifierKey && !IsLockKey)
            {
            }
            else
            {
                if (IsPressed == Visibility.Visible)
                {
                    IsPressed = Visibility.Hidden;
                }
                else
                {
                    IsPressed = Visibility.Visible;
                }
            }
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            if (!IsModifierKey && !IsLockKey)
            {
                IsPressed = Visibility.Hidden;
            }
            base.OnMouseUp(e);
        }
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            IsMouseHover = Visibility.Visible;
            base.OnMouseEnter(e);
        }
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            IsMouseHover = Visibility.Collapsed;
            base.OnMouseLeave(e);
        }
        /// <summary>
        /// Update the text of the key.
        /// </summary>
        public void UpdateDisplayText(bool isShiftKeyPressed, bool isNumLockPressed, bool isCapsLockPressed)
        {
            if (this.IsLetterKey)
            {
                txtValue1.Text = (isShiftKeyPressed ^ isCapsLockPressed) ? NormalText.ToUpper() : NormalText.ToLower();
            }
            else
            {
                if (!IsNumberPadKey)
                {
                    if (IsIMEKey)
                    {
                        txtValue1.Text = NormalText;
                    }
                    else if (!string.IsNullOrEmpty(this.ShiftText))
                    {
                        txtValue1.Text = isShiftKeyPressed ? ShiftText : NormalText;
                        txtValue2.Text = !isShiftKeyPressed ? ShiftText : NormalText;
                    }
                }
                else
                {
                    if (this.IsNumberPadKey)
                    {
                        if (isNumLockPressed)
                        {
                            txtValue1.Text = NormalText;
                            txtValue2.Text = ShiftText;
                        }
                        else
                        {
                            txtValue1.Text = ShiftText;
                            txtValue2.Text = NormalText;

                        }
                    }
                }

            }
        }
        /// <summary>
        /// 获取当前输入法
        /// </summary>
        /// <returns></returns>
        private string GetCultureType()
        {
            return "";
        }
        //用户代码

#endif
    }
}
